package com.moji.controller.web;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.moji.common.base.ApiResult;
import com.moji.entity.*;
import com.moji.entity.vo.CommonAttributeVo;
import com.moji.entity.vo.CommonHomePageDTO;
import com.moji.entity.vo.CommonProductDetailDTO;
import com.moji.entity.vo.MjProductDetailVo;
import com.moji.service.MjHomePageService;
import com.moji.service.MjProductDetailService;
import com.moji.service.MjProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.http.util.TextUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 产品详情 前端控制器
 * </p>
 *
 * @author myg
 * @since 2024-07-23
 */
@RestController
@RequestMapping("/web/mj_product_detail")
@Api(tags = "产品详情相关web接口", value = "产品详情相关web接口")
public class MjProductDetailController {
    /**
     * 服务对象
     */
    @Resource
    private MjProductDetailService mjProductDetailService;

    @Resource
    private MjProductService mjProductService;
    /**
     * 查询产品详情数据
     *
     * @param
     * @return
     */
    @PostMapping("/queryDetailByProductID")
    @ApiOperation(value = "查询产品相关数据,productType不传")
    public ApiResult<List<MjProductDetailVo>> queryDetailByProductID(@RequestBody CommonProductDetailDTO commonProductDetailDTO) {
        List<MjProductDetailVo> mjProductDetailVoList = new ArrayList<>();
        if(commonProductDetailDTO.getProductId()==null)
        {
           if(!TextUtils.isEmpty(commonProductDetailDTO.getRouter()))
           {
               LambdaQueryWrapper<MjProduct> queryWrapper = new LambdaQueryWrapper<>();
               queryWrapper.eq(MjProduct::getRouter, commonProductDetailDTO.getRouter());
               List<MjProduct> list = mjProductService.list(queryWrapper);
               if(list.size()==0||list==null)
               {
                   return ApiResult.routerFailed("查无此router");
               }else{
                   commonProductDetailDTO.setProductId(list.get(0).getId());
               }

           }
        }
        List<MjProductDetail> mjProductDetailList = mjProductDetailService.queryTypeByProductID(commonProductDetailDTO.getProductId());
        for (int i = 0; i < mjProductDetailList.size(); i++) {
            MjProductDetailVo mjProductDetailVo = new MjProductDetailVo();
            mjProductDetailVo.setProductId(mjProductDetailList.get(i).getProductId());
            mjProductDetailVo.setTypeName(mjProductDetailList.get(i).getTypeName());
            mjProductDetailVo.setProductType(mjProductDetailList.get(i).getProductType());
            mjProductDetailVo.setDecrible(mjProductDetailList.get(i).getDecrible());
            mjProductDetailVo.setProductDuration(mjProductDetailList.get(i).getProductDuration());
            mjProductDetailVo.setProductPreferential(mjProductDetailList.get(i).getProductPreferential());
            mjProductDetailVo.setProductDuration(mjProductDetailList.get(i).getProductDuration());
            List<CommonAttributeVo> attributeList = new ArrayList<>();
            commonProductDetailDTO.setProductType(mjProductDetailList.get(i).getProductType());
            List<MjProductDetail> mjBannerMyPage = mjProductDetailService.queryDetailByProductID(commonProductDetailDTO);
            for (int j = 0; j < mjBannerMyPage.size(); j++) {
                CommonAttributeVo commonAttributeVo = new CommonAttributeVo();
                commonAttributeVo.setAttributeName(mjBannerMyPage.get(j).getAttributeName());
                commonAttributeVo.setAttributeDescrible(mjBannerMyPage.get(j).getAttributeDescrible());
                attributeList.add(commonAttributeVo);
            }
            mjProductDetailVo.setAttributeList(attributeList);

            mjProductDetailVoList.add(mjProductDetailVo);
        }


//        List<MjProductDetail> mjBannerMyPage = mjProductDetailService.queryDetailByProductID(commonProductDetailDTO);
//        for (int i = 0; i < mjBannerMyPage.size(); i++) {
//            if(i==0){
//                MjProductDetailVo mjProductDetailVo=new MjProductDetailVo();
//                if(mjBannerMyPage.get(0)!=null)
//                {
//                    mjProductDetailVo.setProductId(mjBannerMyPage.get(0).getProductId());
//                    mjProductDetailVo.setTypeName(mjBannerMyPage.get(0).getTypeName());
//                    mjProductDetailVo.setProductType(mjBannerMyPage.get(0).getProductType());
//                    mjProductDetailVo.setDecrible(mjBannerMyPage.get(0).getDecrible());
//                    mjProductDetailVo.setProductDuration(mjBannerMyPage.get(0).getProductDuration());
//                    mjProductDetailVo.setProductPreferential(mjBannerMyPage.get(0).getProductPreferential());
//                    mjProductDetailVo.setProductDuration(mjBannerMyPage.get(0).getProductDuration());
//
//
//                }
//
//            }
//
//
//        }

        return ApiResult.ok(mjProductDetailVoList);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @PostMapping("/detail")
    @ApiOperation(value = "根据id查看产品详情", response = MjProductDetail.class)
    public ApiResult<MjProductDetail> queryById(@RequestParam("id") Long id) {
        return ApiResult.ok(this.mjProductDetailService.queryById(id));
    }

}

